
#创建连接，查询表中数据
import pymysql
def db_connect():
    db=pymysql.connect('192.168.0.118','root','123456','tinyshop',charset='utf8')
    return db

def db_excute(sql):
    db=db_connect()
    cur=db.cursor()
    cur.execute(sql)
    db.commit()
    return db.cur

#增删改
def change_db(sql):
    db=db_connect()
    cur=db.cursor()
    cur.execute(sql)
    db.commit()
    db.close()

#查询一条数据
def select_one(sql):
    db=db_connect()
    cur=db.cursor()
    cur.execute(sql)
    db.commit()
    r=cur.fetchone()
    db.close()
    return r

#查询所有数据
def select_all(sql):
    db=db_connect()
    cur=db.cursor()
    cur.execute(sql)
    db.commit()
    r=cur.fetchall()
    db.close()
    return r

# r=select_all('select * from user')
# print(r)
# r=select_one('select userpassword from user where username="xiaohong"')
# print(r)

# r=select_one('select id from user order by id desc ')
# print(r)

